
package mail.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import mail.util.Mail;
import mail.util.Reciever;

/**
 * A frame used for reading all new mails for
 * a single user.
 *
 * @author Jonas.Lindmark
 */
public class MailReadFrame extends JInternalFrame implements ActionListener{
    private JPanel msgs = new JPanel();
    private JTextField usr = new JTextField("Your username...");
    private JTextField srvr = new JTextField("mail server");
    private JTextField pass = new JTextField();
    private ArrayList<MailButton> mailButtons = new ArrayList<MailButton>();
    public MailReadFrame()
    {
        super("Read new mail",true,true,true,true);
        setLayout(new BorderLayout());
        setSize(300,300);
        JPanel top = new JPanel();
        top.setLayout(new GridLayout(4,2));
        top.add(new JLabel("Username: "));
        top.add(usr);
        top.add(new JLabel("Server: "));
        top.add(srvr);
        top.add(new JLabel("Password: "));
        top.add(pass);
        JButton update = new JButton("Update");
        update.addActionListener(this);
        update.setActionCommand("update");
        add(update,BorderLayout.SOUTH);
        add(top,BorderLayout.NORTH);

        JScrollPane p = new JScrollPane(msgs);
        msgs.setLayout(new FlowLayout());
        add(p,BorderLayout.CENTER);
        
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

    }
    //handle action events
    public void actionPerformed(ActionEvent event)
    {
        //checks for new mail
        if(event.getActionCommand().equals("update"))
        {
            String password = pass.getText();

            Reciever r = new Reciever();
            if (srvr.getText() == null || srvr.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Input server please.");
                return;
            }
            if (usr.getText() == null || usr.getText().equals(""))
            {
                JOptionPane.showMessageDialog(null, "Input user name please.");
                return;
            }

            r.setHost(srvr.getText());
            r.setLoginID(usr.getText());
            r.setLoginPassword(password);
             
            try{
                //get new emails
                ArrayList<Mail> emails = (ArrayList)r.checkNewMail();
                if (emails.isEmpty())
                {
                    JOptionPane.showMessageDialog(null, "No new messages.");
                } else {
                    //new mails were found so we need to display them
                    System.out.println(new Date().toString() + " : New mail(s) found: " + emails.size());
                    msgs.removeAll();
                    msgs.setLayout(new GridLayout(2,emails.size()/2+1));
                    mailButtons.clear();
                    for (Mail m: emails)
                    {
                        MailButton mb = new MailButton(m);
                        mailButtons.add(mb);
                        msgs.add(mb.getButtonRepresentation());
                    }
                    pack();
                    validate();
                    repaint();
                }

            } catch(Exception e)
            {
                System.out.println(new Date().toString() + " : ERROR : Failed to read mail, username: " + usr.getText() + " server: " + srvr.getText() + " password: " + password);
                e.printStackTrace();
            }

        }
    }
}
